using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ProductService.Application.Services;
using ProductService.Domain.Models;

namespace ProductService.Controllers
{
    /// <summary>
    /// 商品控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ProductController : ControllerBase
    {
        private readonly IProductAppService _productAppService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productAppService">商品应用服务</param>
        public ProductController(IProductAppService productAppService)
        {
            _productAppService = productAppService;
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <returns>商品实体</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<Product>> GetProduct(Guid id)
        {
            var product = await _productAppService.GetProductAsync(id);
            if (product == null)
                return NotFound();
            return Ok(product);
        }

        /// <summary>
        /// 根据分类获取商品
        /// </summary>
        /// <param name="category">商品分类</param>
        /// <returns>商品列表</returns>
        [HttpGet("category/{category}")]
        public async Task<ActionResult<List<Product>>> GetProductsByCategory(string category)
        {
            var products = await _productAppService.GetProductsByCategoryAsync(category);
            return Ok(products);
        }

        /// <summary>
        /// 创建商品
        /// </summary>
        /// <param name="request">创建商品请求</param>
        /// <returns>创建的商品ID</returns>
        [HttpPost]
        public async Task<ActionResult<Guid>> CreateProduct([FromBody] CreateProductRequest request)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var productId = await _productAppService.CreateProductAsync(
                request.Name,
                request.Description,
                request.Price,
                request.Stock,
                request.Category
            );

            return CreatedAtAction(nameof(GetProduct), new { id = productId }, productId);
        }

        /// <summary>
        /// 更新商品
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="request">更新商品请求</param>
        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateProduct(Guid id, [FromBody] UpdateProductRequest request)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            await _productAppService.UpdateProductAsync(
                id,
                request.Name,
                request.Description,
                request.Price,
                request.Category
            );

            return NoContent();
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id">商品ID</param>
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteProduct(Guid id)
        {
            await _productAppService.DeleteProductAsync(id);
            return NoContent();
        }

        /// <summary>
        /// 减少商品库存
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="quantity">减少数量</param>
        [HttpPost("{id}/reduce-stock")]
        public async Task<IActionResult> ReduceStock(Guid id, [FromBody] int quantity)
        {
            if (quantity <= 0)
                return BadRequest("减少数量必须大于0");

            await _productAppService.ReduceStockAsync(id, quantity);
            return NoContent();
        }

        /// <summary>
        /// 增加商品库存
        /// </summary>
        /// <param name="id">商品ID</param>
        /// <param name="quantity">增加数量</param>
        [HttpPost("{id}/increase-stock")]
        public async Task<IActionResult> IncreaseStock(Guid id, [FromBody] int quantity)
        {
            if (quantity <= 0)
                return BadRequest("增加数量必须大于0");

            await _productAppService.IncreaseStockAsync(id, quantity);
            return NoContent();
        }

        /// <summary>
        /// 搜索商品
        /// </summary>
        /// <param name="keyword">搜索关键词</param>
        /// <returns>商品列表</returns>
        [HttpGet("search/{keyword}")]
        public async Task<ActionResult<List<Product>>> SearchProducts(string keyword)
        {
            var products = await _productAppService.SearchProductsAsync(keyword);
            return Ok(products);
        }
    }

    /// <summary>
    /// 创建商品请求
    /// </summary>
    public class CreateProductRequest
    {
        /// <summary>
        /// 商品名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 商品描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 商品价格
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 商品库存
        /// </summary>
        public int Stock { get; set; }

        /// <summary>
        /// 商品分类
        /// </summary>
        public string Category { get; set; }
    }

    /// <summary>
    /// 更新商品请求
    /// </summary>
    public class UpdateProductRequest
    {
        /// <summary>
        /// 商品名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 商品描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 商品价格
        /// </summary>
        public decimal? Price { get; set; }

        /// <summary>
        /// 商品分类
        /// </summary>
        public string Category { get; set; }
    }
}